﻿using M.WWHours.API.Command.WorkerListCommand;
using M.WWHours.Domain.Model;
using M.WWHours.Repository.WWHoursInterface;
using MediatR;

namespace M.WWHours.API.CommandHandlers.WorkerListCommandHandler
{
    /// <summary>
    /// 更新工人状态命令处理器
    /// </summary>
    public class UpdateStatusWorkerListCommandHandler : IRequestHandler<UpdateStatusWorkerListCommand, int>
    {
        /// <summary>
        /// 工人列表仓储接口
        /// </summary>
        protected readonly IWorkerListRepository workerListRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="workerListRepository">工人列表仓储接口</param>
        public UpdateStatusWorkerListCommandHandler(IWorkerListRepository workerListRepository)
        {
            this.workerListRepository = workerListRepository;
        }

        /// <summary>
        /// 更新工人状态命令处理器
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(UpdateStatusWorkerListCommand request, CancellationToken cancellationToken)
        {
            var res = await workerListRepository.GetModelsAsync(request.WorkerListId);
            if (request.Status == "停工")
            {
                res.Status = "已停工";
            }
            return await workerListRepository.UpdateAsync(res);          
        }
    }
}
